package com.app.piemoment.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.app.piemoment.R;
import com.app.piemoment.util.Utils;

public class MyDialog extends Dialog implements android.view.View.OnClickListener{
	private Context context;
	private String msg;
	private TextView title_text;
	private TextView msg_text;
	private Button btn_confirm;
	private Button btn_cancle;
	private String title;
	private OnClickListener onPositiveClickListener;
	private OnClickListener onNegativeClickListener;


	public MyDialog(Context context, String title,String msg) {
		super(context);
		this.title = title;
		this.msg = msg;
		init(context);
	}

	public MyDialog(Context context) {
		super(context, 0);
		init(context);
	}

	private void init(Context context) {
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		Window window = getWindow();
		// window.setBackgroundDrawableResource(android.R.color.transparent);
		ViewGroup viewGroup = (ViewGroup) getLayoutInflater().inflate(
				R.layout.my_dialog, null);
		title_text = (TextView) viewGroup.findViewById(R.id.textView1);
		msg_text = (TextView) viewGroup.findViewById(R.id.textView2);
		btn_confirm = (Button)viewGroup.findViewById(R.id.button1);
		btn_cancle = (Button)viewGroup.findViewById(R.id.button2);
		title_text.setText(title);
		msg_text.setText(msg);
		
		btn_confirm.setOnClickListener(this);
		btn_cancle.setOnClickListener(this);
		
		ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
				Utils.getScreenWidth(context), LayoutParams.WRAP_CONTENT);
		setContentView(viewGroup, params);
	}

	public MyDialog setPositiveButton(OnClickListener onClickListener) {
		this.onPositiveClickListener = onClickListener;
		return this;
	}
	public MyDialog setNegativeButton(OnClickListener onClickListener) {
		this.onNegativeClickListener = onClickListener;
		return this;
	}
	
	
	public interface OnClickListener {
		void onClick(DialogInterface dialog,int which);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			onPositiveClickListener.onClick(this, R.id.button1);
			break;
		case R.id.button2:
			onNegativeClickListener.onClick(this, R.id.button2);
			break;

		default:
			break;
		}
	}
}
